/*
author:杨文超
email:yangwenchao@keynection.cn
usb otg初始化和状态切换函数
*/
#pragma once

#include "sdkconfig.h"
#include "driver/gpio.h"
#include "soc/usb_pins.h"


/* Battery */
#define BSP_BATTERY_VOLTAGE   (GPIO_NUM_2)  // Voltage at this pin = (V_BAT / 2), ADC1 channel 1
#define BSP_BATTERY_VOLTAGE_DIV (2)
#define BSP_BATTERY_BOOST_EN  (GPIO_NUM_13) // 3.3->5V for USB device power from battery. Active high (pulled low)


/**************************************************************************************************
 * ESP32-S3-USB-OTG pinout
 **************************************************************************************************/
/* USB */
#define BSP_USB_POS           USBPHY_DP_NUM
#define BSP_USB_NEG           USBPHY_DM_NUM
#define BSP_USB_MODE_SEL      (GPIO_NUM_18) // Select Host (high level) or Device (low level, default) mode
#define BSP_USB_HOST_VOLTAGE  (GPIO_NUM_1)  // Voltage at this pin = (V_BUS / 3.7), ADC1 channel 0
#define BSP_USB_HOST_VOLTAGE_DIV (3.7f)
#define BSP_USB_LIMIT_EN      (GPIO_NUM_17) // Active high (pulled low)
#define BSP_USB_DEV_VBUS_EN   (GPIO_NUM_12) // Active high (pulled low)

#ifdef __cplusplus
extern "C" {
#endif


/**************************************************************************************************

ESP32-S3-USB-OTG board comes with 3 USB ports:
1. USB-UART0: Only for debugging purposes.
2. USB DEV: USB A Male connector. ESP32-S3 acts as USB device.
3. USB HOST: USB A Female connector. ESP32-S3 acts as USB host.

In ESP32-S3 there is only one USB phy, so USB DEV and USB HOST connectors can't be used at the same time.
To activate one of the two connectors, use functions bsp_usb_mode_select_device() or bsp_usb_mode_select_host().
 **************************************************************************************************/

/*
Switch ESP32-S3-USB-OTG to USB device mode

@return
    - ESP_OK              On success
    - ESP_ERR_INVALID_ARG Parameter error

*/
esp_err_t bsp_usb_mode_select_device(void);

/*
Switch ESP32-S3-USB-OTG to USB host mode
For easy setup of USB host mode use bsp_usb_host_start() function.
Use this in custom USB Host lib configurations.

@return
    - ESP_OK              On success
    - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t bsp_usb_mode_select_host(void);

/*
Power modes of USB Host connector
Use this function only if you want to change power mode on already initialized board,
or in custom USB Host lib configurations.

@note USB Host connector can't be powered from debugging USB port (USB-UART0)
@note If selecting battery mode, the battery slide switch must be switched on
*/
typedef enum bsp_usb_host_power_mode_t {
    BSP_USB_HOST_POWER_MODE_OFF,     //!< No power
    BSP_USB_HOST_POWER_MODE_BATTERY, //!< Power from battery via 3.3->5V boost
    BSP_USB_HOST_POWER_MODE_USB_DEV, //!< Power from USB DEV port
} bsp_usb_host_power_mode_t;

/*
Select power source of USB Host connector

@param[in] mode        USB Host connector power mode
@param[in] limit_500mA Limit output current to 500mA
@return
    - ESP_OK              On success
    - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t bsp_usb_host_power_mode(bsp_usb_host_power_mode_t mode, bool limit_500mA);

/**
Start USB host

This is a one-stop-shop function that will configure the board for USB Host mode
and start USB Host library

@param[in] mode        USB Host connector power mode
@param[in] limit_500mA Limit output current to 500mA
@return
    - ESP_OK                 On success
    - ESP_ERR_INVALID_ARG    Parameter error
    - ESP_ERR_NO_MEM         Memory cannot be allocated
*/
esp_err_t bsp_usb_host_start(bsp_usb_host_power_mode_t mode, bool limit_500mA);

/*
@brief Stop USB host

USB Host lib will be uninstalled and power from connector removed.

@return
    - ESP_OK              On success
    - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t bsp_usb_host_stop(void);

#ifdef __cplusplus
}
#endif
